package com.zx.designpattern.model.http.myvolley;

import android.util.Log;

import com.android.volley.NoConnectionError;
import com.android.volley.TimeoutError;
import com.android.volley.ext.HttpCallback;
import com.android.volley.ext.tools.HttpTools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.logger.Logger;
import com.zx.designpattern.model.http.BaseEntity;
import com.zx.designpattern.model.IBaseModel;
import com.zx.designpattern.utils.ToastUtils;

import java.util.Map;

import static com.android.volley.Request.Method.GET;
import static com.android.volley.Request.Method.POST;
import static com.zx.designpattern.BaseApplication.getAppContext;


/**
 * @作者 zx
 * @创建时间 2018/3/14
 * @描述 volley简单封装
 * @修改时间 2018/3/14
 * @修改描述 volley简单封装
 * @修改者 zx
 **/

public class VolleyManager {

    private static final String TAG = "VolleyManager";

    public static void get(String url, Map<String, String> params, final IBaseModel.CallBack<BaseEntity> mCallBack, final TypeToken typeToken) {
        sendStringRequest(GET, url, params, mCallBack, typeToken);

    }

    public static void post(String url, Map<String, String> params, final IBaseModel.CallBack<BaseEntity> mCallBack, final TypeToken typeToken) {
        sendStringRequest(POST, url, params, mCallBack, typeToken);

    }

    public static final String network_notavaliable = "网络异常，请确认网络正常再进行操作。 ";
    public static final String network_connect_failed = "网络连接超时，请稍后再试。 ";
    public static final String server_connect_failed = "连接服务器失败，请稍后再试。 ";

    private static void sendStringRequest(int medthod, String url, Map<String, String> params, final IBaseModel.CallBack<BaseEntity> mCallBack, final TypeToken typeToken) {
        Logger.e(TAG, "[medthod]" + (medthod == GET ? "GET" : "POST"));
        Logger.e(TAG, "[url]" + url.toString());
        Logger.e(TAG, "[params]" + params.toString());
        HttpTools httptool = new HttpTools(getAppContext());
        httptool.setShouldCache(true);//是否缓存
        HttpCallback mHttpCallback = new HttpCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onResult(String response) {
                Logger.e(TAG, "[response]" + response.toString());
                if (mCallBack != null) {
                    mCallBack.success((BaseEntity) new Gson().fromJson(response, typeToken.getType()));
                }
            }

            @Override
            public void onError(Exception error) {
                Log.e(TAG, error.getMessage(), error);
                if (error instanceof NoConnectionError) {
                    ToastUtils.show(network_notavaliable);
                }
                if (error instanceof TimeoutError) {
                    ToastUtils.show(network_connect_failed);
                }

                if (mCallBack != null) {
                    mCallBack.fail(error);
                }
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onLoading(long count, long current) {

            }
        };
        if (medthod == GET) {
            httptool.get(url, params, mHttpCallback);

        } else {
            httptool.post(url, params, mHttpCallback);

        }

    }


}
